home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Recortar, arrastrar y colocar / ClipViewAll / ClipViewAll.cs next >
Encoding:
Text File  |  2002-05-30  |  6.0 KB  |  179 lines

  1. //------------------------------------------
  2. // ClipViewAll.cs ⌐ 2001 by Charles Petzold
  3. //------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Windows.Forms;
  9.  
  10. class ClipViewAll: Form
  11. {
  12.      Panel       panelDisplay, panelButtons;
  13.      RadioButton radioChecked;
  14.      string[]    astrFormatsSave = new string[0];
  15.      [STAThread] 
  16.  
  17.      public static void Main()
  18.      {
  19.           Application.Run(new ClipViewAll());
  20.      }
  21.      public ClipViewAll()
  22.      {
  23.           Text = "Visor del portapapeles (todos los formatos)";
  24.  
  25.                // Crear panel de ancho variable para mostrar el portapapeles.
  26.  
  27.           panelDisplay = new Panel();
  28.           panelDisplay.Parent = this;
  29.           panelDisplay.Dock = DockStyle.Fill;
  30.           panelDisplay.Paint += new PaintEventHandler(PanelOnPaint);
  31.           panelDisplay.BorderStyle = BorderStyle.Fixed3D;
  32.  
  33.                // Crear el divisor.
  34.  
  35.           Splitter split = new Splitter();
  36.           split.Parent = this;
  37.           split.Dock = DockStyle.Left;
  38.  
  39.                // Crear el panel para los botones de opci≤n.
  40.  
  41.           panelButtons = new Panel();
  42.           panelButtons.Parent = this;
  43.           panelButtons.Dock = DockStyle.Left;
  44.           panelButtons.AutoScroll = true;
  45.           panelButtons.Width = Width / 2;
  46.  
  47.                // Establecer el intervalo en 1 segundo.
  48.  
  49.           Timer timer = new Timer();
  50.           timer.Interval = 1000;
  51.           timer.Tick += new EventHandler(TimerOnTick);
  52.           timer.Enabled = true;
  53.      }
  54.      void TimerOnTick(object obj, EventArgs ea)
  55.      {
  56.           IDataObject data = Clipboard.GetDataObject();
  57.  
  58.           string[] astrFormats = data.GetFormats();
  59.           bool bUpdate = false;
  60.  
  61.                // Determinar si los formatos del portapapeles han cambiado.
  62.  
  63.           if (astrFormats.Length != astrFormatsSave.Length)
  64.                bUpdate = true;
  65.           else
  66.           {
  67.                for (int i = 0; i < astrFormats.Length; i++)
  68.                     if (astrFormats[i] != astrFormatsSave[i])
  69.                     {
  70.                          bUpdate = true;
  71.                          break;
  72.                     }
  73.           }
  74.                // Invalidar la ventana de todos modos.
  75.  
  76.           panelDisplay.Invalidate();
  77.  
  78.                // No actualizar los botones si los formatos no han cambiado.
  79.  
  80.           if (!bUpdate)
  81.                return;
  82.  
  83.                // Los formatos han cambiado; volver a crear los botones de opci≤n.
  84.  
  85.           astrFormatsSave = astrFormats;
  86.           panelButtons.Controls.Clear();
  87.           Graphics grfx = CreateGraphics();
  88.           EventHandler eh = new EventHandler(RadioButtonOnClick);
  89.           int cxText = AutoScaleBaseSize.Width;
  90.           int cyText = AutoScaleBaseSize.Height;
  91.  
  92.           for (int i = 0; i < astrFormats.Length; i++)
  93.           {
  94.                RadioButton radio = new RadioButton();
  95.                radio.Parent = panelButtons;
  96.                radio.Text = astrFormats[i];
  97.  
  98.                if (!data.GetDataPresent(astrFormats[i], false))
  99.                     radio.Text += "*";
  100.  
  101.                try
  102.                {
  103.                     object objClip = data.GetData(astrFormats[i]);
  104.                     radio.Text += " (" + objClip.GetType() + ")";
  105.                }
  106.                catch
  107.                {
  108.                     radio.Text += " (!Excepci≤n en GetData o GetType!)";
  109.                }
  110.                radio.Tag = astrFormats[i];
  111.                radio.Location = new Point(cxText, i * 3 * cyText / 2);
  112.                radio.Size = new Size((radio.Text.Length + 20) * cxText, 
  113.                                      3 * cyText / 2);
  114.                radio.Click += eh;
  115.           }
  116.           grfx.Dispose();
  117.           radioChecked = null;
  118.      }
  119.      void RadioButtonOnClick(object obj, EventArgs ea)
  120.      {
  121.           radioChecked = (RadioButton) obj;
  122.           panelDisplay.Invalidate();
  123.      }
  124.      void PanelOnPaint(object obj, PaintEventArgs pea)
  125.      {
  126.           Panel    panel = (Panel) obj;
  127.           Graphics grfx  = pea.Graphics;
  128.           Brush    brush = new SolidBrush(panel.ForeColor);
  129.  
  130.           if (radioChecked == null)
  131.                return;
  132.  
  133.           IDataObject data = Clipboard.GetDataObject();
  134.           object objClip = data.GetData((string) radioChecked.Tag);
  135.  
  136.           if (objClip == null)
  137.                return;
  138.  
  139.           else if (objClip.GetType() == typeof(string))
  140.           {
  141.                grfx.DrawString((string)objClip, Font, brush, 
  142.                                panel.ClientRectangle);
  143.           }
  144.           else if (objClip.GetType() == typeof(string[]))   // Arrastrar archivo
  145.           {
  146.                string str = string.Join("\r\n", (string[]) objClip);
  147.  
  148.                grfx.DrawString(str, Font, brush, panel.ClientRectangle);
  149.           }
  150.           else if (objClip.GetType() == typeof(Bitmap) ||
  151.                    objClip.GetType() == typeof(Metafile) ||
  152.                    objClip.GetType() == typeof(Image))
  153.           {
  154.                grfx.DrawImage((Image)objClip, 0, 0);
  155.           }
  156.           else if (objClip.GetType() == typeof(MemoryStream))
  157.           {
  158.                Stream stream = (Stream) objClip;
  159.                byte[] abyBuffer = new byte[16];
  160.                long   lAddress = 0;
  161.                int    iCount;
  162.                Font   font = new Font(FontFamily.GenericMonospace, 
  163.                                       Font.SizeInPoints);
  164.                float  y = 0;
  165.  
  166.                while ((iCount = stream.Read(abyBuffer, 0, 16)) > 0)
  167.                {
  168.                     string str = HexDump.ComposeLine(lAddress, abyBuffer, 
  169.                                                                iCount);
  170.                     grfx.DrawString(str, font, brush, 0, y);
  171.                     lAddress += 16;
  172.                     y += font.GetHeight(grfx);
  173.  
  174.                     if (y > panel.Bottom)
  175.                          break;
  176.                }
  177.           }
  178.      }
  179. }